package br.com.fip.sis.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import br.com.fip.sis.model.Conta;
import br.com.fip.sis.model.Transacoes;
import br.com.fip.sis.util.HibernateUtil;
import br.com.fip.sis.util.Log;

public class TransacoesDAO {

	private int idtransacao;

	public void addtransacoes(Transacoes user) {
		Transaction trns = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			trns = session.beginTransaction();
			session.save(user);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (trns != null) {
				trns.rollback();
			}
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
	}

	public void deleteTransacao(int idtransacao) {
		Transaction trns = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			trns = session.beginTransaction();
			Conta id = (Conta) session.load(Conta.class, new Integer(idtransacao));
			session.delete(id);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (trns != null) {
				trns.rollback();
			}
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
	}

	public void updateTransacoes(Transacoes user) {
		Transaction trns = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			trns = session.beginTransaction();
			session.update(user);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			if (trns != null) {
				trns.rollback();
			}
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
	}

	public List<Transacoes> getAllTransacoes() {
		List<Transacoes> transacao = new ArrayList<Transacoes>();
		Transaction trns = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			trns = session.beginTransaction();
			transacao = session.createQuery("from Transacoes").list();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return transacao;
	}

	public Transacoes getContaCaixaById(int transacao) {
		Transacoes transacoes = null;
		Transaction trns = null;
		Session session = HibernateUtil.getSessionFactory().openSession();
		try {
			trns = session.beginTransaction();
			String queryString = "from transacoes where id = :id";
			Query query = session.createQuery(queryString);
			query.setInteger("id", idtransacao);
			transacoes = (Transacoes) query.uniqueResult();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			session.flush();
			session.close();
		}
		return transacoes;
	}
	private Session session;

	@SuppressWarnings("unchecked")
	public List<Transacoes> listarTrasacoesMes(Calendar data, Session session) {
		this.session = session;
		try {
			Calendar dataInicial = Calendar.getInstance();
			Calendar dataFinal = Calendar.getInstance();
			dataInicial.set(Calendar.MONTH, data.get(Calendar.MONTH));
			dataFinal.set(Calendar.MONTH, data.get(Calendar.MONTH));
			dataInicial.set(Calendar.DAY_OF_MONTH, 1);
			dataFinal.set(Calendar.DAY_OF_MONTH,
					dataFinal.getActualMaximum(Calendar.DAY_OF_MONTH));
			Criteria criteria = this.session.createCriteria(Transacoes.class);
			criteria.add(Restrictions.between("data", dataInicial, dataFinal));
			return criteria.list();
		} catch (IllegalArgumentException e) {
			Log.getLogger().error(e);
		} finally {
			session.close();
		}

		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Transacoes> listarTrasacoesDia(Calendar data, Session session) {
		this.session = session;
		try {
			Criteria criteria = this.session.createCriteria(Transacoes.class);
			criteria.add(Restrictions.between("data", data, data));
			return criteria.list();
		} catch (IllegalArgumentException e) {
			Log.getLogger().error(e);
		} finally {
			session.close();
		}

		return null;
	}

}
